package org.admanager.piadlpattern.plugin.diagram.navigator;

import org.admanager.piadladmanagement.plugin.piadladmanagement.CBAPattern;
import org.admanager.piadladmanagement.plugin.piadladmanagement.PComponentPort;
import org.admanager.piadladmanagement.plugin.piadladmanagement.PConnectorRole;
import org.admanager.piadladmanagement.plugin.piadladmanagement.PMultiplicity;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.CBAPatternEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponent2EditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentName2EditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentNameEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentPort2EditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentPortAttachedRoleEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentPortDelegatedPortsEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PComponentPortEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnector2EditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnectorEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnectorName2EditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnectorNameEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnectorRole2EditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnectorRoleDelegatedRolesEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConnectorRoleEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConstraintEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PConstraintNameEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PElementMultiplicityEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PElementRolesEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PMultiplicityEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PRoleEditPart;
import org.admanager.piadlpattern.plugin.diagram.edit.parts.PRoleNameEditPart;
import org.admanager.piadlpattern.plugin.diagram.part.PiADLADManagementMetamodelDiagramEditorPlugin;
import org.admanager.piadlpattern.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry;
import org.admanager.piadlpattern.plugin.diagram.providers.PiADLADManagementMetamodelElementTypes;
import org.admanager.piadlpattern.plugin.diagram.providers.PiADLADManagementMetamodelParserProvider;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class PiADLADManagementMetamodelNavigatorLabelProvider extends
		LabelProvider implements ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		PiADLADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		PiADLADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof PiADLADManagementMetamodelNavigatorItem
				&& !isOwnView(((PiADLADManagementMetamodelNavigatorItem) element)
						.getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof PiADLADManagementMetamodelNavigatorGroup) {
			PiADLADManagementMetamodelNavigatorGroup group = (PiADLADManagementMetamodelNavigatorGroup) element;
			return PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof PiADLADManagementMetamodelNavigatorItem) {
			PiADLADManagementMetamodelNavigatorItem navigatorItem = (PiADLADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (PiADLADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case PConnector2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?PConnector", PiADLADManagementMetamodelElementTypes.PConnector_3013); //$NON-NLS-1$
		case PComponentPort2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?PComponentPort", PiADLADManagementMetamodelElementTypes.PComponentPort_3012); //$NON-NLS-1$
		case PConnectorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?PConnector", PiADLADManagementMetamodelElementTypes.PConnector_2015); //$NON-NLS-1$
		case PComponent2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?PComponent", PiADLADManagementMetamodelElementTypes.PComponent_3011); //$NON-NLS-1$
		case PComponentPortDelegatedPortsEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?PComponentPort?delegatedPorts", PiADLADManagementMetamodelElementTypes.PComponentPortDelegatedPorts_4013); //$NON-NLS-1$
		case PRoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?PRole", PiADLADManagementMetamodelElementTypes.PRole_2018); //$NON-NLS-1$
		case CBAPatternEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://piadladmanagement/1.0?CBAPattern", PiADLADManagementMetamodelElementTypes.CBAPattern_1000); //$NON-NLS-1$
		case PComponentPortEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?PComponentPort", PiADLADManagementMetamodelElementTypes.PComponentPort_2014); //$NON-NLS-1$
		case PElementMultiplicityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?PElement?multiplicity", PiADLADManagementMetamodelElementTypes.PElementMultiplicity_4012); //$NON-NLS-1$
		case PConnectorRoleDelegatedRolesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?PConnectorRole?delegatedRoles", PiADLADManagementMetamodelElementTypes.PConnectorRoleDelegatedRoles_4015); //$NON-NLS-1$
		case PComponentEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?PComponent", PiADLADManagementMetamodelElementTypes.PComponent_2013); //$NON-NLS-1$
		case PConnectorRoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?PConnectorRole", PiADLADManagementMetamodelElementTypes.PConnectorRole_2016); //$NON-NLS-1$
		case PElementRolesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?PElement?roles", PiADLADManagementMetamodelElementTypes.PElementRoles_4011); //$NON-NLS-1$
		case PComponentPortAttachedRoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://piadladmanagement/1.0?PComponentPort?attachedRole", PiADLADManagementMetamodelElementTypes.PComponentPortAttachedRole_4014); //$NON-NLS-1$
		case PMultiplicityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://piadladmanagement/1.0?PMultiplicity", PiADLADManagementMetamodelElementTypes.PMultiplicity_2017); //$NON-NLS-1$
		case PConnectorRole2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?PConnectorRole", PiADLADManagementMetamodelElementTypes.PConnectorRole_3014); //$NON-NLS-1$
		case PConstraintEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://piadladmanagement/1.0?PConstraint", PiADLADManagementMetamodelElementTypes.PConstraint_3015); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = PiADLADManagementMetamodelDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null
				&& elementType != null
				&& PiADLADManagementMetamodelElementTypes
						.isKnownElementType(elementType)) {
			image = PiADLADManagementMetamodelElementTypes
					.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof PiADLADManagementMetamodelNavigatorGroup) {
			PiADLADManagementMetamodelNavigatorGroup group = (PiADLADManagementMetamodelNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof PiADLADManagementMetamodelNavigatorItem) {
			PiADLADManagementMetamodelNavigatorItem navigatorItem = (PiADLADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (PiADLADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case PConnector2EditPart.VISUAL_ID:
			return getPConnector_3013Text(view);
		case PComponentPort2EditPart.VISUAL_ID:
			return getPComponentPort_3012Text(view);
		case PConnectorEditPart.VISUAL_ID:
			return getPConnector_2015Text(view);
		case PComponent2EditPart.VISUAL_ID:
			return getPComponent_3011Text(view);
		case PComponentPortDelegatedPortsEditPart.VISUAL_ID:
			return getPComponentPortDelegatedPorts_4013Text(view);
		case PRoleEditPart.VISUAL_ID:
			return getPRole_2018Text(view);
		case CBAPatternEditPart.VISUAL_ID:
			return getCBAPattern_1000Text(view);
		case PComponentPortEditPart.VISUAL_ID:
			return getPComponentPort_2014Text(view);
		case PElementMultiplicityEditPart.VISUAL_ID:
			return getPElementMultiplicity_4012Text(view);
		case PConnectorRoleDelegatedRolesEditPart.VISUAL_ID:
			return getPConnectorRoleDelegatedRoles_4015Text(view);
		case PComponentEditPart.VISUAL_ID:
			return getPComponent_2013Text(view);
		case PConnectorRoleEditPart.VISUAL_ID:
			return getPConnectorRole_2016Text(view);
		case PElementRolesEditPart.VISUAL_ID:
			return getPElementRoles_4011Text(view);
		case PComponentPortAttachedRoleEditPart.VISUAL_ID:
			return getPComponentPortAttachedRole_4014Text(view);
		case PMultiplicityEditPart.VISUAL_ID:
			return getPMultiplicity_2017Text(view);
		case PConnectorRole2EditPart.VISUAL_ID:
			return getPConnectorRole_3014Text(view);
		case PConstraintEditPart.VISUAL_ID:
			return getPConstraint_3015Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getPComponent_3011Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.PComponent_3011, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(PComponentName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5018); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPElementMultiplicity_4012Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPConstraint_3015Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.PConstraint_3015, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(PConstraintNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5021); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPMultiplicity_2017Text(View view) {
		PMultiplicity domainModelElement = (PMultiplicity) view.getElement();
		if (domainModelElement != null) {
			return String.valueOf(domainModelElement.getLowerBound());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 2017); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponentPortAttachedRole_4014Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPConnector_2015Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.PConnector_2015, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(PConnectorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5020); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPConnectorRole_3014Text(View view) {
		PConnectorRole domainModelElement = (PConnectorRole) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 3014); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponentPort_3012Text(View view) {
		PComponentPort domainModelElement = (PComponentPort) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 3012); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPElementRoles_4011Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPRole_2018Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.PRole_2018, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(PRoleNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5022); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPConnectorRole_2016Text(View view) {
		PConnectorRole domainModelElement = (PConnectorRole) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 2016); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPConnector_3013Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.PConnector_3013, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(PConnectorName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5017); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPConnectorRoleDelegatedRoles_4015Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPComponentPort_2014Text(View view) {
		PComponentPort domainModelElement = (PComponentPort) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 2014); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponent_2013Text(View view) {
		IParser parser = PiADLADManagementMetamodelParserProvider.getParser(
				PiADLADManagementMetamodelElementTypes.PComponent_2013, view
						.getElement() != null ? view.getElement() : view,
				PiADLADManagementMetamodelVisualIDRegistry
						.getType(PComponentNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5019); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getCBAPattern_1000Text(View view) {
		CBAPattern domainModelElement = (CBAPattern) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			PiADLADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponentPortDelegatedPorts_4013Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return CBAPatternEditPart.MODEL_ID
				.equals(PiADLADManagementMetamodelVisualIDRegistry
						.getModelID(view));
	}

}
